<template>
        <div style="background: #fff; width: 700px; margin: 60px auto; padding: 20px 50px;">
            <h1 style="margin: 30px auto; text-align: center">管理系统登录</h1>
            <div class="png">
              <img src="../../assets/logo2.png">
            </div>
            <h2>Managesystem</h2>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" show-password></el-input>
            </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
</template>


<style>
    body {
        background-repeat: no-repeat;
        text-align: center;
    }
    h1{
        font-size: 72px;
        color: black;
        margin-bottom: 0;
    }
    h2{
        font-size: 30px;
        color: black;
        margin-bottom: 50px;
    }
    png{
        width: 100px;
        padding-top: 50px;
    }
</style>

<script>
    export default {
        data(){
            return{
                ruleForm:{
                    username:'张三',
                    password:'123456',
                },
                rules:{
                    username:[
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 15, message: '用户名长度必须在 2 到 15 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 4, max: 15, message: '密码长度必须在 4 到 15 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            //用于登录
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                      let url = 'http://localhost:8080/user/login';
                      console.log('url = ' + url);

                      let formData = this.qs.stringify(this.ruleForm);
                      console.log('formData = ' + formData);

                      this.axios.post(url, formData).then((response) => {
                        let jsonResult = response.data;
                        if (jsonResult.state == 20000) {
                          // 登录成功,弹出提示
                          this.$message({
                            message: '登录成功！',
                            type: 'success'
                          });
                          // 将服务器端响应的JWT存储到localStorage
                          let jwt = jsonResult.data;
                          console.log('jwt = ' + jwt);
                          localStorage.setItem('jwt', jwt);
                          console.log('已经将JWT存储到localStorage');
                          // 跳转：到主页
                          this.$router.push('/');
                        } else if (jsonResult.state == 40100) {
                          // 用户名或密码错误
                          this.$message.error(jsonResult.message);
                        } else if (jsonResult.state == 40101) {
                          // 账号状态错误
                          this.$alert(jsonResult.message, '登录失败', {
                            confirmButtonText: '确定',
                            callback: action => {}
                          });
                        } else {
                          // 其它错误
                          this.$alert(jsonResult.message, '登录失败', {
                            confirmButtonText: '确定',
                            callback: action => {}
                          });
                        }
                      });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
          resetForm(formName) {
            this.$refs[formName].resetFields();
          }
        }
    }
</script>
